<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>Document</title>
    <script src="../vue.js"></script>
  </head>

  <body>
    <div id="app">
      <input type="text" v-model="search" />
      <ul>
        <li v-for="item in filterPersons" :key="item.id">
          name---{{item.name}}---sex---{{item.sex}}
        </li>
      </ul>
    </div>
    <script>
      Vue.config.productionTip = false;

      const vm = new Vue({
        el: "#app",
        data() {
          return {
            persons: [
              { id: 1, name: "周冬雨", sex: "女" },
              { id: 2, name: "马冬梅", sex: "女" },
              { id: 3, name: "周杰伦", sex: "男" },
              { id: 4, name: "林俊杰", sex: "男" },
              { id: 5, name: "马云", sex: "男" },
            ],
            search: "",
            filterPersons: [],
          };
        },
        watch: {
          search: {
            //立即监听
            immediate: true,
            handler() {
              this.filterPersons = this.persons.filter((item) =>
                item.name.includes(this.search)
              );
            },
          },
        },
      });
    </script>
  </body>
</html>
